<?php session_start();
		include('includes/header.php');
		include("../includes/config.php"); 
		  
		
		  
		if($_POST['submit'] ){
			if(isset($_POST['file'])){
				$allowedExts = array("jpg", "jpeg", "gif", "png");
				$extension = end(explode(".", $_FILES["file"]["name"]));
				if ((($_FILES["file"]["type"] == "image/gif")
				|| ($_FILES["file"]["type"] == "image/jpeg")
				|| ($_FILES["file"]["type"] == "image/pjpeg"))
				&& ($_FILES["file"]["size"] < 2000000)
				&& in_array($extension, $allowedExts))
				  {
				  if ($_FILES["file"]["error"] > 0)
					{
					echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
					}
				  else
					{
					/* echo "Upload: " . $_FILES["file"]["name"] . "<br />";
					echo "Type: " . $_FILES["file"]["type"] . "<br />";
					echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
					echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; */
						$imgPath =  "http://". $_SERVER['SERVER_NAME'].	 "/vlieg/images/background-images/".$_FILES["file"]["name"];
						
						$maxO = mysql_fetch_array(mysql_query("select max(img_order) as imgOrder from `background-images`")); 
						$maxO = $maxO[imgOrder]  + 1;
						$price = mysql_real_escape_string($_POST['price']);
						$city = mysql_real_escape_string($_POST['city']);
						$text = mysql_real_escape_string($_POST['text']);
						
						$q = "update`background-images` set url='$imgPath', price='$price', city= '$city', text='$text' where id_image='$_GET[imageId]'";
								
							mysql_query($q) or die(mysql_error());
							
						$gelukt = "<h2>Achtergrond gewijzigd</h2>";
					
					  move_uploaded_file($_FILES["file"]["tmp_name"],
					  "../images/background-images/" . $_FILES["file"]["name"]);
					  echo "Stored in: " . "../images/background-images/" . $_FILES["file"]["name"];
					  
					}
				  }
				else
				  {
				  echo "Invalid file";
				  }
			}else{
				$price = mysql_real_escape_string($_POST['price']);
				$city = mysql_real_escape_string($_POST['city']);
				$text = mysql_real_escape_string($_POST['text']);
						
				$q = "update`background-images` set  price='$price', city= '$city', text='$text' where id_image='$_GET[imageId]'";
					
				mysql_query($q) or die(mysql_error());
				$gelukt = "<h2>Achtergrond gewijzigd</h2>";
			}
		}
		if(isset($_GET['imageId'])){
			$query ="select id_image, url, img_order, city,text, price FROM `background-images` where id_image = ".$_GET['imageId'];
			$arr = mysql_fetch_array(mysql_query($query)) or die(mysql_error());	
		}
		
?>

     <div  id="main" role="main" style="font-family:arial">
		<h1>Achtergrondafbeelding wijzigen</h1>
		<?php if (isset($gelukt)){
			echo $gelukt;
		}?>
			<form action="#" method="post"	enctype="multipart/form-data">
				<img src="<?php echo $arr['url']; ?>" height="100"/><br/>
				<label for="file">Filename:</label>
				<input type="file" name="file" id="file" /> <br/>
					<label for="city">Stad:</label>
				<input type="text" name="city" value="<?php echo $arr['city']; ?> " id="city" /> <br/>
					<label for="text">Text:</label>
				<input type="text" name="text"  value="<?php echo $arr['text']; ?> "id="text" /> <br/>
					<label for="text">Prijs:</label>
				<input type="text" name="price"  value="<?php echo $arr['price']; ?> "id="price" /> <br/>
				<br />
				<input type="submit" name="submit" value="Submit" />
			</form>
     </div>

  
  
<?php include('includes/footer.php');?>